// 渲染浏览记录列表
function renderHistory() {
    const historyList = document.querySelector('.history-list');
    const historyCount = document.getElementById('history-count');
    
    // 更新记录数量
    historyCount.textContent = historyData.length;
    
    // 渲染记录列表
    historyList.innerHTML = historyData.map(item => `
        <div class="history-item" data-id="${item.id}">
            <div class="item-image">
                <img src="${item.image}" alt="${item.title}">
            </div>
            <div class="item-info">
                <div class="item-title">${item.title}</div>
                <div class="item-spec">${item.spec}</div>
                <div class="item-price">¥${item.price}</div>
                <div class="item-date">浏览时间：${formatDate(item.date)}</div>
            </div>
            <div class="item-actions">
                <button class="btn-delete" onclick="deleteHistory(${item.id})">删除</button>
            </div>
        </div>
    `).join('');
}

// 格式化日期
function formatDate(dateString) {
    const date = new Date(dateString);
    return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日 ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`;
}

// 删除记录
function deleteHistory(id) {
    if (confirm('确定要删除这条浏览记录吗？')) {
        historyData = historyData.filter(item => item.id !== id);
        renderHistory();
        showToast('删除成功');
    }
}

// 显示提示信息
function showToast(message) {
    const toast = document.createElement('div');
    toast.className = 'toast-message';
    toast.textContent = message;
    
    document.body.appendChild(toast);
    
    setTimeout(() => {
        toast.classList.add('fade-out');
        setTimeout(() => {
            document.body.removeChild(toast);
        }, 300);
    }, 2000);
}

// 页面加载完成后渲染浏览记录
document.addEventListener('DOMContentLoaded', renderHistory); 